<?php
namespace App\Store\Services;

use App\Store\Models\Account\AccountMenu;
use App\Store\Models\Account\AccountRole;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class AccountMenuRole
{
    public static function setAccountRole($account_id,$acc_type=9){
        $select_arr = ['id', 'pid','keyIndex', 'name', 'path', 'props','hidden', 'component', 'always_show', 'title', 'icon', 'no_cache', 'link', 'is_botton', 
            //  'is_default as is_checked',
             DB::raw('(if(FIND_IN_SET('.$acc_type.',rule_typestr),1,0)) as is_checked')
            ];
            $menulist = AccountMenu::where(['pid' => 0])->WhereRaw('FIND_IN_SET(?,default_rule_typestr)', [$acc_type])
                ->select($select_arr)->get();
    
    
            foreach ($menulist as &$val) {
                $buttonList = AccountMenu::where(['pid' => $val->id, 'is_botton' => 1])->WhereRaw('FIND_IN_SET(?,default_rule_typestr)', [$acc_type])->select($select_arr)->get();
                $val->meta = ['title' => $val->title, 'icon' => $val->icon,'buttonList'=>$buttonList];
                unset($val->title);
                unset($val->icon);
                $submenu = AccountMenu::where(['pid' => $val->id, 'is_botton' => 0])->WhereRaw('FIND_IN_SET(?,default_rule_typestr)', [$acc_type])->select($select_arr)->get();
                $val->props=$val->props?json_decode($val->props,true):[];
                foreach ($submenu as &$vval) {
                    $vval_buttonList = AccountMenu::where(['pid' => $vval->id, 'is_botton' => 1])->WhereRaw('FIND_IN_SET(?,default_rule_typestr)', [$acc_type])->select($select_arr)->get();
                    $vval->meta = ['title' => $vval->title, 'icon' => $vval->icon,'buttonList'=>$vval_buttonList];
                    $vval->props=$vval->props?json_decode($vval->props,true):[];
    
                    unset($vval->title);
                    unset($vval->icon);
                    

                    $subsubmenu = AccountMenu::where(['pid' => $vval->id, 'is_botton' => 0])->WhereRaw('FIND_IN_SET(?,default_rule_typestr)', [$acc_type])->select($select_arr)->get();
                    
                    foreach ($subsubmenu as &$vvval) {
                        $vvval_buttonList = AccountMenu::where(['pid' => $vvval->id, 'is_botton' => 1])->WhereRaw('FIND_IN_SET(?,default_rule_typestr)', [$acc_type])->select($select_arr)->get();
                        
                        $vvval->meta = ['title' => $vvval->title, 'icon' => $vvval->icon,'buttonList'=>$vvval_buttonList];
                        $vvval->props=$vvval->props?json_decode($vvval->props,true):[];
                        
                        unset($vvval->title);
                        unset($vvval->icon);
                        
                    }
                    $vval->children =$subsubmenu;
                }
                $val->children = $submenu;
            }
        if($account_id==0){//超管
            //设置人员权限redis缓存
    
            Redis::set('account_user_menulist_Account_0', json_encode($menulist));
        }else{
            Redis::set('account_user_menulist_Account_'.$account_id, json_encode($menulist));
            $roleinfo=AccountRole::where(['account_id' => $account_id])->first();
            if($roleinfo){

                AccountRole::where(['account_id' => $account_id])->update(['menu_ruleinfo' => json_encode($menulist)]);
            }else{
                AccountRole::create(['account_id' => $account_id,'menu_ruleinfo' => json_encode($menulist)]);
            }
        }
    }
}